﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DeepSeekDLL;

namespace Demo2
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }


        string connectionString = "Server=localhost;Database=Deepseek;User Id=sa;Password=123456;";
        string model = "deepseek-reasoner";
        int recordId = 0;
        string apikey = "sk-485cdd201bcf4e7584244f0e93415bfa";
        string baseurl = "https://api.deepseek.com";


        /// <summary>
        /// 同步新增
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Button1_Click(object sender, EventArgs e)
        {
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

            //构造完整url
            var apiUrl = "" + baseurl + "/chat/completions";

            try
            {
                Button1.Enabled = false;
                for (var i = 0; i < 2; i++)
                {
                    DeepSeekService ds = new DeepSeekService();
                    string result = ds.ds1(connectionString, apikey, model, TextArea1.InnerText, 0, apiUrl);
                    TextArea2.InnerText = result;
                }
            }
            catch (Exception ex)
            {
                TextArea2.InnerText = "异常：" + ex.Message;
            }
            finally
            {
                Button1.Enabled = true;
            }
        }

        /// <summary>
        /// 同步更新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Button2_Click(object sender, EventArgs e)
        {
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
            //构造完整url
            var apiUrl = "" + baseurl + "/chat/completions";

            try
            {
                Button3.Enabled = false;
                for (var i = 0; i < 2; i++)
                {
                    int id = 0;
                    using (SqlConnection conn = new SqlConnection(connectionString))
                    {
                        conn.Open();
                        string query = "INSERT INTO ewaichat(chatext1) "
                            + " VALUES(@chatext1 ); "
                            + "SELECT SCOPE_IDENTITY()";

                        using (SqlCommand cmd = new SqlCommand(query, conn))
                        {
                            cmd.Parameters.AddWithValue("@chatext1", TextArea1.InnerText);

                            id = Convert.ToInt32(cmd.ExecuteScalar());
                        }
                    }

                    DeepSeekService ds = new DeepSeekService();
                    string result = ds.ds2(connectionString, apikey, model, TextArea1.InnerText, id, apiUrl, "ewaichat", "chatext2");

                    TextArea2.InnerText = result;
                }
            }
            catch (Exception ex)
            {
                TextArea2.InnerText = "异常：" + ex.Message;
            }
            finally
            {
                Button3.Enabled = true;
            }
        }

        /// <summary>
        /// 异步新增
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected async void Button3_Click(object sender, EventArgs e)
        {
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

            //构造完整url
            var apiUrl = "" + baseurl + "/chat/completions";

            try
            {
                Button2.Enabled = false;

                for (var i = 0; i < 2; i++)
                {
                    DeepSeekService ds = new DeepSeekService();
                    string result = await ds.ds1Sync(connectionString, apikey, model, TextArea1.InnerText, 0, apiUrl);
                    TextArea2.InnerText = result;
                }
            }
            catch (Exception ex)
            {
                TextArea2.InnerText = "异常：" + ex.Message;
            }
            finally
            {
                Button2.Enabled = true;
            }
        }

        /// <summary>
        /// 异步更新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected async void Button4_Click(object sender, EventArgs e)
        {
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

            //构造完整url
            var apiUrl = "" + baseurl + "/chat/completions";

            try
            {
                for (var i = 0; i < 2; i++)
                {
                    Button4.Enabled = false;

                    int id = 0;
                    using (SqlConnection conn = new SqlConnection(connectionString))
                    {
                        conn.Open();
                        string query = "INSERT INTO ewaichat(chatext1) "
                            + "VALUES (@chatext1); "
                            + "SELECT SCOPE_IDENTITY(); ";

                        using (SqlCommand cmd = new SqlCommand(query, conn))
                        {
                            cmd.Parameters.AddWithValue("@chatext1", TextArea1.InnerText);

                            id = Convert.ToInt32(cmd.ExecuteScalar());
                        }
                    }

                    DeepSeekService ds = new DeepSeekService();
                    string result = await ds.ds2Sync(connectionString, apikey, model, TextArea1.InnerText, id, apiUrl, "ewaichat", "chatext2");

                    TextArea2.InnerText = result;
                }
            }
            catch (Exception ex)
            {
                TextArea2.InnerText = "异常：" + ex.Message;
            }
            finally
            {
                Button4.Enabled = true;
            }
        }
    }
}